package com.cloud.spock.groovy.test

import static org.junit.Assert.*

import com.cloud.spock.test.MyService
import org.junit.Test
import org.junit.runner.RunWith
import spock.lang.Specification

/**
 * JUnit 的测试用例总是由 Runner 去执行
 * Specification的注解：@RunWith(Sputnik.class) 是对 org.junit.runner.Runner的扩展
 */
class MyServiceTest extends Specification{
	//groovy里面用def定义所有的对象，包含方法声明
	def mockRequestNo = "123"
	def "最基本的测试:mock返回值"() {
		given: "given用来准备mock对象，可以放到when里面"
		MyService myService = Stub(MyService)
		and:"and声明可选"
		myService.getValue() >> "1"
		when: "when里面调用被测试的方法"
		String result = myService.getValue()
		then: "then用来验证结果"
		result == "1"
	}

	def "最基本的测试:多次调用返回不同值"() {
		given: "返回多个值使用三个>"
		MyService myService = Stub(MyService)
		myService.getValue() >>> ["1", "2", "3"]
		//except相当于when then的合并
		expect:
		myService.getValue() == "1"
		myService.getValue() == "2"
		myService.getValue() == "3"
	}
	def "测试模糊匹配"() {
		given:
		MyService myService = Stub(MyService)
		//下划线表示匹配所有输入值
		myService.stringToInteger(_) >> 999
		//		myService.stringToInteger("1") >> 999
		//except相当于when then的合并
		expect: "有时候我们并不在乎输入值"
		myService.stringToInteger("1") == 999
		myService.stringToInteger("2") == 999
		myService.stringToInteger("3") == 999
	}
}
